package com.example.mytestdemo.extensions

import android.graphics.LinearGradient
import android.graphics.Shader
import android.widget.TextView
import androidx.annotation.ColorRes

fun TextView.linearGradient(@ColorRes startColorRes: Int, @ColorRes endColorRes: Int, vertical: Boolean = true) {
    val startColor = context.getCompatColor(startColorRes)
    val endColor = context.getCompatColor(endColorRes)
    val gradient = if (vertical) LinearGradient(0f,0f,0f, paint.descent() - paint.ascent(), startColor, endColor, Shader.TileMode.CLAMP) else LinearGradient(0f,0f, measuredWidth.toFloat(), 0f, startColor, endColor, Shader.TileMode.CLAMP)
    paint.shader = gradient
    invalidate()
}